/*
程序完成的任务设计方案：即拷贝源文件到目标文件
一、询问目标文件名称的名称并打开它
二、使用一个循环询问源文件
三、以读模式依次打开每个源文件，并将其添加到目标文件末尾

细化打开文件步骤：
1.以附加模式打开目标文件
2.如果打开失败，则退出程序
3.为该文件创建一个4096字节的缓冲区
4.如果创建失败，则退出程序

细化拷贝部分：
1.如果该文件与目标文件相同，则跳至下一个文件
2.如果以读模式无法打开文件，则跳至下一个文件
3.把文件内容添加加至目标文件

最后，程序回到文件开始处，显示当前整个文件内容
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LIM 80
#define BUFSIZE 4096

void append(FILE * soucre, FILE * dest);
char * s_gets(char * st, int n);

int main(void)
{
    char fname[LIM]; //目标文件名
    char cpname[LIM];     //源文件名
    int count = 0;   //统计拷贝文件数
    FILE * fa;       //指向目标文件
    FILE * fs;       //指向源文件
    int ch;
    

    printf("Enter the file name what you want to open: ");
    s_gets(fname, LIM);
    if ((fa = fopen(fname, "a+")) == NULL) //打开目标文件
    {
        fprintf(stderr, "Can't open %s!\n", fname);
        exit(EXIT_FAILURE);
    }

    if (setvbuf(fa, NULL, _IOFBF, BUFSIZE)) //创建缓冲区
    {
        fputs("Output buffer can't be created!\n", stderr);
        exit(EXIT_FAILURE);
    }


    printf("Enter the file name what you want to copy (enmpty line to quit):");

    while (s_gets(cpname, LIM) && cpname[0] != '\0')
    {
        if (strcmp(fname, cpname) == 0)
        {
            fprintf(stderr, "file name is repeat.\n");
            printf("Enter the file name what you want to copy (enmpty line to quit):");
            continue;
        }
        else if ((fs = fopen(cpname, "r")) == NULL)
        {
            fprintf(stderr, "Can't open %s by read mode!", cpname);
            printf("Enter the file name what you want to copy (enmpty line to quit):");
            continue;
        } 
        else 
        {
            if (setvbuf(fs, NULL, _IOFBF, BUFSIZE))
            {
                fputs("Input buffer can't be careated!\n", stderr);
                printf("Enter the file name what you want to copy (enmpty line to quit):");
                continue;
            }
            append(fs, fa);
            if (ferror(fs) != 0)
                fprintf(stderr, "Error in reading file %s.\n", cpname);
            if (ferror(fa) != 0)
                fprintf(stderr, "Error in writing file %s.\n", fname);
            
            fclose(fs);
            count++;
            printf("File %s appended.\n", cpname);
            printf("Enter the file name what you want to copy (enmpty line to quit):");    

        }        
        
        
        
    }
    printf("Done appending. %d files has appended.\n", count);
    
    rewind(fa);
    printf("%s contents:\n", fname);
    while ((ch = getc(fa)) != EOF)
        putchar(ch);
    puts("\nDone displaying!");
    
    fclose(fa);

    return 0;
}

void append(FILE * source, FILE * dest)
{
    size_t bytes;
    static char temp[BUFSIZE];

    while ((bytes = fread(temp, sizeof(char), BUFSIZE, source)) > 0)
        fwrite(temp, sizeof(char), bytes, dest);

}
char * s_gets(char * st, int n)
{
    char * ret_val;
    int i = 0;
    
    ret_val = fgets(st, LIM, stdin);

    if (ret_val)
    {
        while (st[i] != '\n' && st[i] != '\0')
            i++;
    }

    if(st[i] == '\n')
        st[i] = '\0';
    else
    {
        while (getchar() != '\n')
            continue;
    }
    
    return ret_val;
}